package zpy.servlet.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.*;

/**
 * Servlet implementation class ReadHtmlFileServlet
 */
@WebServlet("/ReadHtmlFileServlet")
public class ReadHtmlFileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReadHtmlFileServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		Writer writer =  response.getWriter();
		String filePath = getServletContext().getRealPath("/WEB-INF/html/ImageCarousel.html");
		File file = new File(filePath);
		if (!file.exists()) {
			writer.write("文件不存在");
			writer.close();
			return;
		}
		Reader reader = new FileReader(file);
		char[] buf = new char[8192];
		int len = 0;
		while((len = reader.read(buf))!=-1){
			writer.write(buf, 0, len);
		}
		reader.close();
		writer.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
